using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SiliverlightJavascriptIntelli
{
    public static class SilverlightJavascriptIntellisenseRemover
    {
        private static readonly List<RegexToReplaceWithGroup> regexes = new List<RegexToReplaceWithGroup>();

        static SilverlightJavascriptIntellisenseRemover()
        {
            regexes.Add(new RegexToReplaceWithGroup(@"\.get_([^(\()]*)(\()(\))", @".$1"));
            regexes.Add(new RegexToReplaceWithGroup(@"\.set_(?!item)([^(\()]*)\(((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!)))\)", @".$1 = $2"));
            regexes.Add(new RegexToReplaceWithGroup(@"Convert\.To[^(\()]*\(((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!)))\)", @"$1"));
            regexes.Add(new RegexToReplaceWithGroup(@"Convert\.Is[^(\()]*\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\)", @"true"));
            regexes.Add(new RegexToReplaceWithGroup(@"(\w*)\.createFromXaml\(((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!)))\)", @"$2.content.createFromXaml(""<$1 />"")"));
            regexes.Add(new RegexToReplaceWithGroup(@"new\s[^(\()]*\(((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!)))\)", "$1"));
            regexes.Add(new RegexToReplaceWithGroup(@"\.add_([^(\()]*)\(((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!)))\)", @".addEventListener(""$1"", $2)"));
            regexes.Add(new RegexToReplaceWithGroup(@"\.remove_([^(\()]*)\(((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!)))\)", @".removeEventListener(""$1"", $2)"));
        }

        public static string RemoveFrom(string input)
        {
            foreach (RegexToReplaceWithGroup curReplace in regexes)
            {
                input = curReplace.regex.Replace(input, curReplace.replaceWith);
            }

            return input;
        }
    }
}
